package com.chinadci.smartcommunitysystembackstage.controller;


import com.chinadci.smartcommunitysystembackstage.entity.ScsAdmin;
import com.chinadci.smartcommunitysystembackstage.entity.ScsOwner;
import com.chinadci.smartcommunitysystembackstage.entity.ScsPay;
import com.chinadci.smartcommunitysystembackstage.service.ScsActivityService;
import com.chinadci.smartcommunitysystembackstage.service.ScsOwnerService;
import com.chinadci.smartcommunitysystembackstage.service.ScsPayService;
import com.chinadci.smartcommunitysystembackstage.util.DateTime;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Code Duck
 * @since 2021-04-13
 */
@RestController
@CrossOrigin
@Api("建业E家")
public class ScsPayController {
    @Autowired
    private ScsPayService service;

    @Autowired
    private ScsOwnerService owner;

    //显示所有用户缴费记录
    @ApiOperation(value = "显示所有用户缴费记录")           //swagger
    @ApiImplicitParam(name = "name",value = "业主名称")       //表明要传入的参数
    @RequestMapping(value = "/api/find",method = RequestMethod.GET)
    public Map Find(String name){
        Map<String,Object> map=new HashMap<>();
        map.put("data",service.Find(name));
        return map;
    }

    //删除用户缴费记录
    @ApiOperation(value = "删除用户缴费记录")           //swagger
    @ApiImplicitParam(name = "id",value = "缴费记录id")       //表明要传入的参数
    @RequestMapping(value = "/api/delpay",method = RequestMethod.POST)
    public Map Del(int id){
        Map<String,Object> map=new HashMap<>();
        map.put("data",service.Del(id));
        return map;
    }

    //用户缴费
    @ApiOperation(value = "新增用户缴费记录")           //swagger
    @ApiImplicitParam(name = "ScsPay",value = "对象")       //表明要传入的参数
    @RequestMapping(value = "/api/xzpay",method = RequestMethod.POST)
    public Map Insert(ScsPay pay){
        Map<String,Object> map=new HashMap<>();
        if (owner.finds(pay.getName())==null){
            map.put("name",0);      //0的时候该用户名输入有误
            return map;
        }else{
            int id=owner.finds(pay.getName()).getOId();
            pay.setOId(id);
            pay.setPCreateTime(DateTime.GetTime());
            String s = "";
            while(s.length()<18) {
                s += (int) (Math.random() * 10);
            }
            pay.setPNumber(s);
            map.put("data",service.Xz(pay));
        }
        return map;
    }
}
